import os
from openpyxl import load_workbook 
import shutil

dir0 = "Transaction_20241110030000"
dir1 = "Transaction_20241109030000"

xls_name = "q_lost.xlsx"
q_lost = "q_lost"
q_lost00 = "q_lost00"

def excel_files(dir):
    wb = load_workbook(xls_name)
    sheets = wb.worksheets
    print(sheets)

    sheet1 = sheets[0]
    print(sheet1[1])

    rows = sheet1.rows
    for row in rows:
        row_val = [col.value for col in row]
        print(row_val)
        # copy_files(dir,row_val[1])

def copy_files(dir,equ_name):
    for path in os.listdir(dir):
        if equ_name in path :
            print(path)
            shutil.copy(dir + "/" + path, q_lost + "/" + path)

# 通过订单号找
def excel_files00(dir):
    wb = load_workbook(xls_name)
    sheets = wb.worksheets
    print(sheets)

    sheet1 = sheets[0]
    print(sheet1[1])
    # row1 = []
    # for row in sheet1[1]:
    #     print(row)
    #     row1.append(row.value)
    # print(row1)
    rows = sheet1.rows
    for row in rows:
        row_val = [col.value for col in row]
        print(row_val)
        # 取设备交易时间符合文件
        move_files00(dir,row_val[0],row_val[1])

# 找设备订单号符合文件
def move_files00(dir,order_no,equ_name):
    for path in os.listdir(dir):
        if equ_name in path :
            # 解析文件
            path_file = dir + '/' + path
            with open(path_file, 'rb') as file:
                content = file.read()
            hex_str = content.hex()
            
            if order_no in hex_str:
                print(path)
                print(hex_str)
                if os.path.exists(path_file):
                    os.rename(path_file, q_lost00 + "/" + path)

excel_files00(q_lost)

        
